﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Services;
using System.Collections;
using BLL;
using Model;

namespace GeniusWebApplication
{
    public partial class self : System.Web.UI.Page
    {
        public User user = null;

        public string visitID = null;//此空间的会员

        public string myEmail = null;//传到前台的email
        public string hisEmail = null;//传到前台的对方email

        public string methodId = null;//我的上传1，我的收藏2，他的上传3 我的下载4

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)HttpContext.Current.Session["user"];
            if (user == null)
                Response.Redirect("log.aspx");


            if (null == Request.QueryString["toname"])
            {
                visitID = user.id.ToString();
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=1'>我的上传</a></span>";
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=4'>我的下载</a></span>";
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=2'>我的收藏</a></span>";
            }
            else
            {
                visitID = Request.QueryString["toname"];   ///得到本空间会员的ID
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=1'>我的上传</a></span>";
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=4'>我的下载</a></span>";
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=2'>我的收藏</a></span>";
                this.lib_serve.InnerHtml = this.lib_serve.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='self.aspx?methodId=3&toname=" + visitID + "'>TA的上传</a></span>";
            }


            if (null != Request.QueryString["methodId"])
                methodId = Request.QueryString["methodId"];
            else methodId = "1";

            UserRelationService urs = new UserRelationService();
            //我的关注列表
            IList<UserRelation> iur = urs.GetUserRelations(user.username);
            this.relation_who.InnerHtml = "<div id='title_who'>我关注的</div>";
            if (0 == iur.Count)
            {
                this.relation_who.InnerHtml = this.relation_who.InnerHtml + "<div class='relationItem'><p>暂无关注列表</p></div>";
            }
            else
            {
                string u = null;
                foreach (UserRelation ur in iur)
                {
                    u = ur.follow.name;
                    this.relation_who.InnerHtml = this.relation_who.InnerHtml + "<div class='relationItem'><a href='self.aspx?toname=" + ur.follow.id + "'><p>" + u + "</p></a></div>";
                }
            }



            //关注我的列表
            iur = urs.GetUserRelationsFollow(user.username);
            this.relation_who1.InnerHtml = "<div id='title_who1'>关注我的</div>";
            if (0 == iur.Count)
            {
                this.relation_who1.InnerHtml = this.relation_who1.InnerHtml + "<div class='relationItem1'><p>暂无关注列表</p></div>";
            }
            else
            {
                string u = null;
                foreach (UserRelation ur in iur)
                {
                    u = ur.who.name;
                    this.relation_who1.InnerHtml = this.relation_who1.InnerHtml + "<div class='relationItem1'><a href='self.aspx?toname=" + ur.who.id + "'><p>" + u + "</p></a></div>";
                }
            }

            //人员信息
            User theuser = new UserService().getUserById(int.Parse(visitID));
            iur = urs.GetUserRelationsFollow(theuser.username);//关注此人的
            this.order.InnerHtml = "<img src='./img/kai.png' title='天才海伦'/>";

            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><p>ID      ：" + theuser.name + "</p></div>";
            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><p>邮箱    ：" + theuser.username + "</p></div>";
            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><p>所属部门：" + theuser.department.name + "</p></div>";

            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><p>被关注  ：" + iur.Count + "</p></div>";
            //this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><p>上传：231</p></div>";
            this.order.InnerHtml = this.order.InnerHtml + "<span class='ordersItem'><input id='btn_concern' value='关注TA' onclick='clickConcern();' type='button'/></span>";
            this.order.InnerHtml = this.order.InnerHtml + "<span class='ordersItem'><input id='btn_concern_cancel' value='取消关注' onclick='clickConcern_cancel();' type='button'/></span>";

            myEmail = user.username;
            hisEmail = theuser.username;


            //我的上传1，我的收藏2，他的上传3 我的下载4
            ResourceService resource = new ResourceService();
            IList<Resource> resourcelist = null;
            if (methodId.Equals("1"))
            {
                resourcelist = resource.getResourceByUser(user);
                if (0 == resourcelist.Count)
                    this.myListUl.InnerHtml = "<li><span class='myListUl-in'></span><span >您尚未上传资源</span> </li>";
                else
                    foreach (Resource rs in resourcelist)
                    {
                        if(1==rs.type)
                            if(rs.status==-1)
                            {this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>未通过</span></li>";}
                            else if (rs.status == 0)
                            {this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>审核中</span></li>";}
                            else
                            {this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>审核通过</span></li>";}

                        else if(2==rs.type)
                            if (rs.status == -1)
                            { this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>未通过</span></li>"; }
                            else if (rs.status == 0)
                            { this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>审核中</span></li>"; }
                            else
                            { this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>审核通过</span></li>"; }
                        else if (3 == rs.type)
                            if (rs.status == -1)
                            { this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>未通过</span></li>"; }
                            else if (rs.status == 0)
                            { this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>审核中</span></li>"; }
                            else
                            { this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> <span class='concuring'>审核通过</span></li>"; }
                    }
            }
            else if (methodId.Equals("2"))
            {   
                FavoriteService favorite = new FavoriteService();
                IList<Favourite> favoritelist = favorite.getUserFavourite(user.id);
                if (0 == favoritelist.Count)
                    this.myListUl.InnerHtml = "<li><span class='myListUl-in'></span><span >您尚未收藏资源</span> </li>";
                else
                    foreach (Favourite fav in favoritelist)
                    {
                        if (1 == fav.resource.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + fav.resource.id + "'>" + fav.resource.resourceName + "</a></span> </li>";
                        else if (2 == fav.resource.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + fav.resource.id + "'>" + fav.resource.resourceName + "</a></span> </li>";
                        else if (3 == fav.resource.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + fav.resource.id + "'>" + fav.resource.resourceName + "</a></span> </li>";

                    }
            }
            else if (methodId.Equals("3"))
            {
                resourcelist = resource.getResourceByUser(theuser);
                if (0 == resourcelist.Count)
                    this.myListUl.InnerHtml = "<li><span class='myListUl-in'></span><span >此会员尚未上传资源</span> </li>";
                else
                    foreach (Resource rs in resourcelist)
                    {
                        if (1 == rs.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> </li>";
                        else if (2 == rs.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> </li>";
                        else if (3 == rs.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + rs.id + "'>" + rs.resourceName + "</a></span> </li>";

                    }
            }
            else if (methodId.Equals("4"))
            {
                DownloadHistoryService downloadHistory = new DownloadHistoryService();
                IList<DownloadHistory> downloadHistorylist = downloadHistory.getUserDownloadHistory(user.id);
                if (0 == downloadHistorylist.Count)
                    this.myListUl.InnerHtml = "<li><span class='myListUl-in'></span><span >您尚未下载资源</span> </li>";
                else
                    foreach (DownloadHistory dlh in downloadHistorylist)
                    {
                        if (1 == dlh.resource.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + dlh.resource.id + "'>" + dlh.resource.resourceName + "</a></span> </li>";
                        else if (2 == dlh.resource.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + dlh.resource.id + "'>" + dlh.resource.resourceName + "</a></span> </li>";
                        else if (3 == dlh.resource.type)
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + dlh.resource.id + "'>" + dlh.resource.resourceName + "</a></span> </li>";

                    }
            }

        }
        [WebMethod]
        public static string ajaxGetMessageNum(int uid)
        {
            NoticeService ds = new NoticeService();
            IList<Notice> dl = ds.getUserNotReadNotice(uid);
            int l = dl.Count();
            return "{" +
                    "result:'" + l + "'" +
                    "}";
        }
        /// <summary>
        /// 添加关注
        /// </summary>
        /// <param name="who"></param>
        /// <param name="follow"></param>
        /// <returns></returns>
        [WebMethod]
        public static string ajaxAddRelationFunc(string who, string follow)
        {
            UserRelationService urs = new UserRelationService();
            Hashtable ht = urs.addRelation(who, follow);
            string result = (string)ht["result"];
            return "{" +
                    "result:'" + result + "'" +
                    "}";
        }

        /// <summary>
        /// 取消关注
        /// </summary>
        /// <param name="who"></param>
        /// <param name="follow"></param>
        /// <returns></returns>
        [WebMethod]
        public static string ajaxDelRelationFunc(string who, string follow)
        {
            UserRelationService urs = new UserRelationService();
            Hashtable ht = urs.DelRelation(who, follow);
            string result = (string)ht["result"];
            return "{" +
                    "result:'" + result + "'" +
                    "}";
        }
    }
}